package org.itboys.core.controller.web.tray;

import com.wordnik.swagger.annotations.Api;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.common.UserTypeEnum;
import org.itboys.core.dto.tray.SundayTrayDto;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mysql.tray.SundayTrayModel;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.service.mysql.web.SundayTrayModelService;
import org.itboys.core.service.mysql.web.SundayTrayService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/12/5
 * 描述：托盘
 */
@Controller
@RequestMapping("/sunday/web/tray")
@Api(value = "tray", description = "托盘")
public class SundayTrayController extends BaseController {
    @Autowired
    private SundayTrayService trayService;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayTrayModelService modelService;

    /**
     * 主页
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model
    ) {
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("userType", UserTypeEnum.SUPPLIER.getCode());
        List<SundayMember> members = memberService.list(param);
        model.addAttribute("companys", members);
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        List<SundayTrayModel> models = modelService.select(param);
        model.addAttribute("models", models);
        return "/sunday/tray/index";
    }

    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            Model model
    ) {
        return "/sunday/tray/input";
    }


    /**
     * 分页查询
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response
            ,@RequestParam(value = "ids",required = false)List<Long> ids
    ) {
        PageResult<SundayTrayDto> result = trayService.select(request,ids);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }


    @RequestMapping("/batchSave")
    public void batchSave(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value = "modelId", required = true) Long modelId,
            @RequestParam(value = "modelName", required = true) String modelName,
            @RequestParam(value = "num", required = true) Integer num
    ) {
        trayService.batchSave(modelId,modelName, num);
        ResponseMessage.success();
    }

}
